package com.example.administrator.myapp;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Toast;

import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.github.lzyzsd.jsbridge.DefaultHandler;

public class MainActivity extends AppCompatActivity {
    private BridgeWebView mWebView = null;
    private String url = null;

    private Context mContext;

    ValueCallback<Uri> mUploadMessage;

    ValueCallback<Uri[]> mUploadMessageArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        mContext = this;
        mWebView = this.findViewById(R.id.myWebview);


        /***********************=====定义插件====*************************/
        mWebView.setDefaultHandler(new DefaultHandler());

        mWebView.setWebChromeClient(new WebChromeClient() {

            @SuppressWarnings("unused")
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String AcceptType, String capture) {
                this.openFileChooser(uploadMsg);
            }

            @SuppressWarnings("unused")
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String AcceptType) {
                this.openFileChooser(uploadMsg);
            }

            public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                mUploadMessage = uploadMsg;
            }

            @Override
            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
                mUploadMessageArray = filePathCallback;
                return true;
            }
        });

        // 设置文件地址
        url = "file:///android_asset/www/index.html";

        // 加载页面
        mWebView.loadUrl(url);

        /**
         * 监听JS发过来的请求
         */
        mWebView.registerHandler("btnClick", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                // 获取手机型号
                Toast.makeText(mContext, data + " | 手机型号：" + android.os.Build.BRAND, Toast.LENGTH_LONG).show();
                Log.e("webviewPlugin", data);
                function.onCallBack("webviewPlugin");
            }
        });

        mWebView.registerHandler("getVersion", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                try {
                    String RELEASE = android.os.Build.VERSION.RELEASE;
                    String BRAND = android.os.Build.BRAND;
                    PackageManager manager = mContext.getPackageManager();
                    PackageInfo info = manager.getPackageInfo(mContext.getPackageName(), 0);
                    String name = info.versionName;
                    Toast.makeText(mContext, RELEASE + " | " + BRAND + " | " + name, Toast.LENGTH_SHORT).show();
                } catch (Exception e) {

                }

            }
        });

        /**
         * 向JS发送请求
         */
        mWebView.callHandler("jsMethod", "info from Android", new CallBackFunction() {
            @Override
            public void onCallBack(String data) {
                Toast.makeText(mContext, data, Toast.LENGTH_LONG).show();
            }
        });
    }

}
